<template>
  <div class="container">
    <div class="app-container">
      <el-tree default-expand-all :data="depts" :props="defaultPrors" :expand-on-click-node="false">
        <template v-slot="{data}">
          <el-row style="width: 100%;height: 40px;" type="flex" justify="space-between" align="middle">
            <el-col :span="20">{{ data.name }}</el-col>
            <el-col :span="4">
              <span class="tree-manager"> {{ data.managerName }}</span>
              <!-- $event 实参 表示类型 -->
              <!-- @command="operateDept"绑定点击事件 -->
              <el-dropdown @command="operateDept($event,data.id)">
                <!-- 显示区域内容 -->
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <!-- 下拉菜单选项 -->
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>
      <!-- 添加弹层 -->
      <!-- :visible +  @update:visible 简写 :visible.sync  -->
      <add-dept ref="addDeptRef" :p-node-id="pNodeId" :visible.sync="showDialog" @updateDepts="getDepartment" />
    </div>
  </div>
</template>
<script>
import { delDepartmentApi, getDepartmentApi } from '@/api/department'
import { transListToTreeData } from '@/utils'
import AddDept from '@/layout/components/add-dept.vue'

export default {
  name: 'Department',
  components: {
    AddDept
  },
  data() {
    return {
      depts: [],

      // 2.默认属性
      defaultPrors: {
        children: 'children',
        label: 'name'
      },
      showDialog: false, // 控制弹层的显示/隐藏
      pNodeId: null// 存储当前id

    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    // 获取部门
    async getDepartment() {
      const res = await getDepartmentApi()
      // console.log(res)
      // console.log(transListToTreeData(res, 0))
      this.depts = transListToTreeData(res, 0)// 把对象数组转成树形数组
    },
    // 弹框事件
    operateDept(type, pid) {
      // console.log(type)
      // 1.添加
      if (type === 'add') {
        this.pNodeId = pid// 记录id
        this.showDialog = true// 显示弹层
      }
      // 2. 编辑
      if (type === 'edit') {
        // this.pNodeId = pid
        this.showDialog = true
        // 调用子组件中的获取部门详情的方法
        this.$nextTick(() => {
          this.$refs.addDeptRef.getDepartmentDetail(pid)
        })
      }
      // 3.删除
      if (type === 'del') {
        this.$confirm('您确定要删除该部门吗').then(async() => {
          await delDepartmentApi(pid)
          this.$message.success('删除部门成功~')
          this.getDepartment()// 获取部门列表
        })
      }
    }
  }
}
</script>

<style scoped lang="scss">
.app-container {
  padding: 30px 140px;
  font-size: 14px;
}
.tree-manager {
  width: 80px;
  display: inline-block;
  margin: 10px;
}
</style>
